﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Store.WP7.ViewModels;
using Microsoft.Phone.Controls;

namespace Store.WP7.Controls
{
    public class StoreApplicationPage : PhoneApplicationPage
    {
        protected PageViewModelBase ViewModelBase;

        public StoreApplicationPage()
        {
            this.Loaded += new RoutedEventHandler(OnStoreNavigationPageLoaded);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            ViewModelBase = DataContext as PageViewModelBase;  
            if (ViewModelBase != null)
            {
                ViewModelBase.OnNavigatedTo(NavigationService, NavigationContext, State, e);
            }
            base.OnNavigatedTo(e);
        }

        private void OnStoreNavigationPageLoaded(object sender, RoutedEventArgs e)
        {
            if (ViewModelBase != null)
            {
                ViewModelBase.OnLoaded();
            }
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (ViewModelBase != null)
            {
                ViewModelBase.OnBackKeyPress(e);
            }
            base.OnBackKeyPress(e);
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            if (ViewModelBase != null)
            {
                ViewModelBase.OnNavigatingFrom(e, State);
            }
            base.OnNavigatingFrom(e);
        }
    }
}
